﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ECCIT;
using ECCIT.Data;
using ECCIT.Business.Model;
using ECCIT.Business.DAL;

using OgilvyOne.Model;
using OgilvyOne.SqlServerDAL;
using OgilvyOne.UKNow.Globalization;
using OgilvyOne.UKNow.Utility;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.IDAL;
using System.Data;
using OgilvyOne.UKNow.Common;

namespace OgilvyOne.UKNow.DAL
{
    public class EventsDateDAL: UKNowBaseDAL,IDAlEventsDate
    {
        private const string TBL_NAME = "UKNOW_EVENTS_DATE";                //表名 活动时间表
        private const string COL_LANG_ID = "COL_LANG_ID";                   // 语言
        private const string COL_EVENTS_ID = "COL_EVENTS_ID";              //活动ID
        private const string COL_EVENTS_DATE = "COL_EVENTS_DATE";          //活动时间

        public override string TableName { get { return TBL_NAME; } }

        public override Type ModelType { get { return typeof(EventsDateModel); } }
        [PrimaryKey]
        public IField LanguageID { get { return FieldFactory.Create(ModelType, "LanguageID", ColumnFactory.CreateColumn(COL_LANG_ID, DataType.Int)); } }
        [PrimaryKey]
        public IField ID { get { return FieldFactory.Create(ModelType, "ID", ColumnFactory.CreateColumn(COL_EVENTS_ID, DataType.Int)); } }
        [PrimaryKey]
        public IField Time { get { return FieldFactory.Create(ModelType, "Time", ColumnFactory.CreateColumn(COL_EVENTS_DATE, DataType.String,10)); } }

        public override IField[] InsertFields { get { return new IField[] { LanguageID, ID, Time }; } }
        public override IField[] SelectFields { get { return new IField[] { LanguageID, ID, Time }; } }
        public override IField[] UpdateFields { get { return new IField[] { LanguageID, ID, Time }; } }

        public EventsDateDAL() { }

        public EventsDateDAL(LanguageType languageType) { this.Language = LanguageFactory.Create(languageType); }

        #region IDAL成员
        /// <summary>
        /// 保存修改数据
        /// </summary>
        /// <param name="model">活动时间实体</param>
        /// <returns></returns>
        public ResultInfo Save(EventsDateModel model) {

            if (model == null) throw new ArgumentNullException("model");
            ResultInfo res = new ResultInfo(false);
            try
            {
                //Delete(Convert.ToInt32(model.ID));
                int flag=Insert(model, null);
                if (flag > 0)
                {
                    res.Message = "保存成功";
                    res.Result = true;
                    res.ReturnObject = model;
                }
            }
            catch (Exception e)
            {
                res.Exception = e;
                res.Message = string.Format("保存数据时发生异常。异常信息：{0}", e.Message);
            }
            return res;
        }

        /// <summary>
        /// 根据活动Id 删除所有该活动的时间数据
        /// </summary>
        /// <param name="eventsId">活动ID</param>
        /// <returns>返回ResultInfo对象</returns>
        public int Delete(int eventsId)
        {
            string strSql = string.Format("delete from {0} where {1}={2} and COL_LANG_ID={3}", TableName, COL_EVENTS_ID, eventsId,(int)this.Language.LanguageType);
            object obj = DBHelper.ExecuteScalar(strSql);
            int flag = 0;
            if (obj != null && obj != DBNull.Value)
                flag = (int)obj;
            return flag;

            //using (IDbConnection conn = DBHelper.GetDbConnection())
            //{
            //    conn.Open();
            //    IDbTransaction trans = conn.BeginTransaction();
            //    try
            //    {
            //        DeleteModelByPrimaryKey(trans, this.GetType(), new KeyValuePair<IField, object>[] { new KeyValuePair<IField, object>(LanguageID, (int)this.Language.LanguageType), new KeyValuePair<IField, object>(this.ID, eventsId) });
            //        trans.Commit();
            //        result.Result = true;
            //        result.Message = Language.GetAlertMessage(ModelType, Enums.Action.Delete, Enums.ActionResult.Success);
            //    }
            //    catch (Exception ex)
            //    {
            //        trans.Rollback();
            //        result.Message = Language.GetAlertMessage(ModelType, Enums.Action.Delete, Enums.ActionResult.Exception, ex);
            //    }
            //    finally
            //    {
            //        trans.Dispose();
            //    }
            //}
        }
        #endregion
    }
}
